package com.yyk.yiliao.ui.trade;

import android.graphics.Color;
import android.os.Bundle;
import com.google.android.material.textfield.TextInputEditText;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.widget.Button;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.util.BtnToEditListenerUtil;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.ToastUtil;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.bean.UserbankCodecardInfo;

import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class AddBankcard2Activity extends BaseActivity {

    public static AddBankcard2Activity intance;

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_cardname)
    TextInputEditText etCardname;
    @BindView(R.id.et_card)
    TextInputEditText etCard;
    @BindView(R.id.et_phone)
    TextInputEditText etPhone;
    @BindView(R.id.bt_next)
    Button btNext;
    private String cardname; //卡号名称
    private String card;//卡号

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_bankcard2;
    }

    @Override
    public void initView() {
        initToolbar();
        initTextView();
        initButton();


    }

    private void initButton() {
        BtnToEditListenerUtil.getInstance().setBtn(btNext).setBackgroud(R.color.blue_51, R.color.blue_89)
                .addEditView(etCard)
                .addEditView(etCardname)
                .addEditView(etPhone)
                .build();
    }

    private void initTextView() {
        //更改颜色 Android中TextView中部分文字有不同颜色的实现 - 丿灬安之若死 - CSDN博客  https://blog.csdn.net/mp624183768/article/details/78722298
        SpannableString spannableString = new SpannableString("请添加账户本人银行卡");
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#148DFF")), spannableString.length() - 7, spannableString.length() - 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvTitle.setText(spannableString);
    }

    private void initToolbar() {
        setBackArrow();
        setTitle("添加银行卡");
    }

    @Override
    public void initData() {
        intance = this;
        initGetIntent();
    }

    private void initGetIntent() {
        if (getBundle() != null) {
            cardname = getBundle().getString("cardname");
            card = getBundle().getString("card");
            etCardname.setText(cardname);
            etCard.setText(card);
        }

    }


    @OnClick(R.id.bt_next)
    public void onViewClicked() {
        String phone = etPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            ToastUtil.showShort(mActivity, "手机号码不能为空");
            return;
        }

        Map<String, String> map = new TreeMap<>();
        map.put("id", Hawk.get("uid") + "");
        map.put("cnumber", card);
        map.put("tel", phone);
        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postUserbankCodecard(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<UserbankCodecardInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(UserbankCodecardInfo info) {
                        //正确输入不打印出来值
                        Logger.e("绑卡银行信息第三步" + info.toString());
                        if (info.getCode() == 1) {
                            ToastUtil.showShort(mActivity, "短信发送成功");
                            Bundle bundle = new Bundle();
                            bundle.putString("merchantFlowId", info.getData().getMerchantFlowId() + "");
                            bundle.putString("phone", phone + "");
                            bundle.putString("type", 1 + "");
                            if (AddBankcardActivity.intance != null) {
                                AddBankcardActivity.intance.finish();
                            }
                            startAct(AddBankcard3Activity.class, true, bundle);
                        } else if (info.getCode() == 111) {
                            ToastUtil.showShort(mActivity, "绑卡成功");
                            if (AddBankcardActivity.intance != null) {
                                AddBankcardActivity.intance.finish();
                            }
                            finish();
                        } else if (info.getCode() == 200) {
//                            ToastUtil.showShort(mActivity, "短信发送失败");
                            Bundle bundle = new Bundle();
                            bundle.putString("merchantFlowId", info.getData().getMerchantFlowId() + "");
                            bundle.putString("phone", phone + "");
                            bundle.putString("type", 200 + "");
                            if (AddBankcardActivity.intance != null) {
                                AddBankcardActivity.intance.finish();
                            }
                            startAct(AddBankcard3Activity.class, true, bundle);
                        } else if (info.getCode() == 146) {
                            ToastUtil.showShort(mActivity, "手机号和绑定的银行卡信息不匹配");
                        } else if (info.getCode() == 119) {
                            ToastUtil.showShort(mActivity, "该卡号已经绑定");
                        } else if (info.getCode() == 0) {
                            ToastUtil.showShort(mActivity, "请输入正确的手机号码");
                        }else if (info.getCode() == 147) {
                            ToastUtil.showShort(mActivity, "超过失败次数，请明日再试");

                        }
                    }
                });


    }
}
